import os

# ✅ 设置你的根目录
root_dir = r"C:\Users\宋孟林\Project\IntelliJ IDEAProjects\AppProject\lease\web\web-admin\src\main\java\org\example\lease\vo"  # <-- 修改为你的目标路径
target_extension = ".java"

start_prefix = "package"
end_prefixes = ("@Data", "@Schema")  # 支持多个结束标志

for dirpath, dirnames, filenames in os.walk(root_dir):
    for filename in filenames:
        if filename.endswith(target_extension):
            file_path = os.path.join(dirpath, filename)

            with open(file_path, 'r', encoding='utf-8') as f:
                lines = f.readlines()

            new_lines = []
            inside_delete_block = False
            start_found = False

            for line in lines:
                stripped = line.strip()

                # 找到 package 开头的起始行
                if not start_found and stripped.startswith(start_prefix):
                    new_lines.append(line)
                    start_found = True
                    inside_delete_block = True
                    continue

                # 找到结束行，满足 public 或 class 开头
                if inside_delete_block and any(stripped.startswith(prefix) for prefix in end_prefixes):
                    new_lines.append(line)
                    inside_delete_block = False
                    continue

                # 非删除区内容保留
                if not inside_delete_block:
                    new_lines.append(line)

            # 写回处理后的内容
            with open(file_path, 'w', encoding='utf-8') as f:
                f.writelines(new_lines)

            print(f"✅ Processed: {file_path}")

print("🎉 全部处理完成！")
